import Mock from 'mockjs'
import user from './user.js'
import log from './log.js'
import node from './node.js'
// Mock.mock('/node/queryList','post',()=>{
//   return node.nodeList
// })
// Mock.mock('/log/queryLogList','post',()=>{
//   return log.LogList
// })
// Mock.mock('/user/queryAllUserList', 'post', () => {
//   return user.UserList
// })
// Mock.mock('/user/useradd', 'post', (params) => {
//     const obj = hsGetParams(params.body)
//     let list = {}
//     for (let i in obj) {
//         list[obj[i].key] = obj[i].value
//     }
//     console.log(list);
//     list.create_time = formatGTM()
//     user.UserList.data.list.splice(0, 0, list)
//     return user.UserList
// })
// Mock.mock('/user/usermodify', 'post', (params) => {
//     const obj = hsGetParams(params.body)
//     let list = {}
//     for (let i in obj) {
//         list[obj[i].key] = obj[i].value
//     }
//     console.log(list);
//     for (let i = 0; i < user.UserList.data.list.length; i++) {
//         if (list.id == user.UserList.data.list[i].id) {
//             let date = user.UserList.data.list[i].create_time
//             user.UserList.data.list[i] = list
//             user.UserList.data.list[i].create_time = date
//         }
//     }
//     return user.UserList
// })
// Mock.mock('/user/userdelete', 'post', (params) => {
//     const id = hsGetParams(params.body)[0].value
//     // let list = {}
//     // for (let i in obj) {
//     //     list[obj[i].key] = obj[i].value
//     // }
//     // console.log(list);
//     for (let i = 0; i < user.UserList.data.list.length; i++) {
//         if (id == user.UserList.data.list[i].id) {
//             user.UserList.data.list.splice(i, 1)
//             break;
//         }
//     }
//     return user.UserList
// })
// Mock.mock('/user/queryDataList', 'post', (params) => {
//   const searchName = decodeURI(getParams(params.body)[2].value)
//   console.log(searchName)
//   const list = user.UserList.data.list.filter((item) => {
//     return item.user_name.indexOf(searchName) > -1
//   })
//   console.log(list);
//   return {
//     status: 200,
//     msg: 'success',
//     data: {
//       list: list
//     }
//   }
// })
Mock.mock('/root/login', 'post', (params) => {
  const obj = getParams(params.body)
  console.log(obj)
  if (obj[0].value === 'admin' && obj[1].value === '123456') {
    return user.userInfo
  } else {
    return {
      data: {},
      status: 200,
      msg: '帐号或密码错误'
    }
  }
})

Mock.mock('/login/logout', 'post', () => {
  return {
    code: 200,
    status: 200,
    msg: '帐号或密码错误'
  }
})
function getParams (params) {
  const obj = params.split('&')
  const result = []
  for (let i = 0; i < obj.length; i++) {
    result.push({
      key: obj[i].split('=')[0],
      value: obj[i].split('=')[1]
    })
  }
  return result
}

function hsGetParams (params) {
    const obj = params.split('&')
    const result = []
    for (let i = 0; i < obj.length; i++) {
        result.push({
            key: obj[i].split('=')[0],
            value: decodeURI(obj[i].split('=')[1])
        })
    }
    return result
}

function formatSix() {
    var date = new Date()
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? ('0' + m) : m
    var d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    let time = y + '-' + m + '-' + d
    return time
}

function formatGTM(){
    var date = new Date()
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? ('0' + m) : m
    var d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    var h = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    h = h < 10 ? ('0' + h) : h
    minute = minute < 10 ? ('0' + minute) : minute
    second = second < 10 ? ('0' + second): second
    let time = y + '-' + m + '-' + d+' '+h+':'+ minute + ':' + second
    return time
}
